package com.thirdsdk.jpush;

import java.util.LinkedHashSet;
import java.util.Set;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import com.orangelife.mobile.util.LogE;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

public class JPushTagHelper {
    private Context context;
    private static final int MSG_SET_ALIAS = 1001;
    private static final int MSG_SET_TAGS = 1000;
    private static JPushTagHelper getTagInfo = null;

    public static JPushTagHelper getInstance() {
        if (getTagInfo == null) {
            getTagInfo = new JPushTagHelper();
        }
        return getTagInfo;
    }

    private String replaceInvalid(String str) {
        if (!TextUtils.isEmpty(str) && str.contains("-")) {
            str = str.replaceAll("-", "_");
        }
        return str;
    }

    public void setTag(String... tag) {
        if (tag != null) {
            Set<String> tagSet = new LinkedHashSet<>();
            for (String t : tag) {
                if (!TextUtils.isEmpty(t)) {
                    replaceInvalid(t);
                    if (TagValidateUtil.isValidTagAndAlias(t)) {
                        tagSet.add(t);
                    } else {
                        LogE.E(JPushTagHelper.class.getSimpleName(), "InValidTag " + t);
                    }
                }
            }
            if (tagSet.size() > 0) {
                handler.sendMessage(handler.obtainMessage(MSG_SET_TAGS, tagSet));
            } else {
                LogE.E(JPushTagHelper.class.getSimpleName(), "EmptyTags");
            }
        }
    }

    public void setAlias(String alias) {
        if (!TextUtils.isEmpty(alias)) {
            replaceInvalid(alias);
            if (TagValidateUtil.isValidTagAndAlias(alias)) {
                handler.sendMessage(handler.obtainMessage(MSG_SET_ALIAS, alias));
            } else {
                LogE.E(JPushTagHelper.class.getSimpleName(), "InValidAlias " + alias);
            }
        } else {
            LogE.E(JPushTagHelper.class.getSimpleName(), "EmptyAlias!");
        }

    }

    public Context setContext(Context context) {
        if (this.context == null) {
            this.context = context;
        }
        return this.context;
    }


    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case MSG_SET_ALIAS:
                    LogE.E(JPushTagHelper.class.getSimpleName(), "MSG_SET_ALIAS = " + msg.obj);
                    JPushInterface.setAliasAndTags(context, (String) msg.obj,
                            null, mAliasCallback);
                    break;
                case MSG_SET_TAGS:
                    LogE.E(JPushTagHelper.class.getSimpleName(), "MSG_SET_TAGS = " + msg.obj);
                    JPushInterface.setAliasAndTags(context, null,
                            (Set<String>) msg.obj, mTagsCallback);
                    break;
            }

        }
    };

    private TagAliasCallback mTagsCallback = new TagAliasCallback() {

        @Override
        public void gotResult(int code, String arg1, Set<String> tags) {
            switch (code) {
                case 0:
                    LogE.e(JPushTagHelper.class.getSimpleName(), "Set tags success");
                    break;

                case 6002:
                    LogE.e(JPushTagHelper.class.getSimpleName(), "Failed to set tags due to timeout. Try again after 60s.");
                    handler.sendMessageDelayed(
                            handler.obtainMessage(JPushTagHelper.MSG_SET_TAGS, tags),
                            1000 * 60);
                    break;
                default:
                    LogE.e(JPushTagHelper.class.getSimpleName(), "Failed with errorCode = " + code);
            }
        }
    };

    private final TagAliasCallback mAliasCallback = new TagAliasCallback() {

        @Override
        public void gotResult(int code, String alias, Set<String> tags) {
            switch (code) {
                case 0:
                    LogE.e(JPushTagHelper.class.getSimpleName(), "Set alias success");
                    break;
                case 6002:
                    LogE.e(JPushTagHelper.class.getSimpleName(), "Failed to set alias due to timeout. Try again after 60s.");
                    handler.sendMessageDelayed(handler.obtainMessage(MSG_SET_ALIAS, alias), 1000 * 60);
                    break;
                default:
                    LogE.e(JPushTagHelper.class.getSimpleName(), "Failed with errorCode = " + code);
            }
        }

    };

}
